﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FacadeSingTowerMall;
using DTOSingTowerMall;

namespace WebFormSingTowerMall.Approval
{
    public partial class PromotionsApproval : System.Web.UI.Page
    {
        PromotionFacade pf = new PromotionFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PromotsGridView.DataSource = pf.getPromos();
                PromotsGridView.DataBind();
                if (PromotsGridView.Rows.Count == 0)
                    Label1.Text = "No any application now";
            }
        }

        protected void PromotsGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            PromotsGridView.SelectedRow.Cells[6].Text = "Approved";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < PromotsGridView.Rows.Count; i++)
            {
                if (PromotsGridView.Rows[i].Cells[6].Text == "Approved")
                {
                    int campaignId = Convert.ToInt32(PromotsGridView.Rows[i].Cells[0].Text);
                    Label1.Text = campaignId.ToString();
                    ICampaign promots = pf.getPromotsData(campaignId).First();
                    promots.Status = "Approved";
                    pf.getPromotsApproval();
                }
                else if (PromotsGridView.Rows[i].Cells[6].Text == "Rejected")
                {
                    int campaignId = Convert.ToInt32(PromotsGridView.Rows[i].Cells[0].Text);
                    Label1.Text = campaignId.ToString();
                    ICampaign promots = pf.getPromotsData(campaignId).First();
                    promots.Status = "Rejected";
                    pf.getPromotsApproval();
                }
                Label1.Text = "Update Successful";
            }
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((Button)sender).Parent.Parent;
            PromotsGridView.SelectedIndex = row.RowIndex;
            PromotsGridView.SelectedRow.Cells[6].Text = "Rejected";
            Label1.Text = null;
        }
    }
}